//
//  TeacherLeftView.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/4/8.
//  Copyright © 2019 李文强. All rights reserved.
//

#import "TeacherLeftView.h"
#import "MinButtonViewBtn.h"
#define W 185*fw
#define H 40*fh
@implementation TeacherLeftView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame andButtonView:(NSArray *)array andImageArray:(NSArray *)imageArray{
    if ([super initWithFrame:frame]) {
        self.frame = CGRectMake(0, 20, 215*fw, APP_HEIGHT-20);
        [self.buttonArray addObjectsFromArray:array];
        [self.imageArray addObjectsFromArray:imageArray];
        [self initSubView];
    }
    return self;
}
- (void)initSubView{
    self.backgroundColor = MAINBACKCOLOR;
    [self addSubview:self.openButton];
   
    for (int i = 0; i<self.buttonArray.count; i++) {
        UIButton *mineButton = [UIButton buttonWithType:UIButtonTypeCustom];
        mineButton.frame = CGRectMake(15,44*fh+(H+10)*i, W-20, H);
        NSString *title = [NSString stringWithFormat:@" %@",_buttonArray[i]];
        NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:title attributes:@{NSFontAttributeName: [UIFont fontWithName:@"PingFang-SC-Regular" size: 15],NSForegroundColorAttributeName: TITLECOLOR}];
        [mineButton setAttributedTitle:string forState:UIControlStateNormal];
        mineButton.tag = 10+i;
        mineButton.layer.cornerRadius = 5;
        mineButton.layer.masksToBounds = YES;
        [mineButton setImage:[UIImage imageNamed:self.imageArray[i]] forState:0];
        mineButton.selected = NO;
        [mineButton setBackgroundImage:[UIImage imageWithColor:[UIColorChange colorwithHexString:@"#DDDDDD"] forSize:CGSizeMake(100, 100)] forState:UIControlStateSelected];
        [mineButton setBackgroundImage:[UIImage imageWithColor:[UIColor clearColor] forSize:CGSizeMake(100, 100)] forState:UIControlStateNormal];
        [mineButton addTarget:self action:@selector(mineClick:) forControlEvents:UIControlEventTouchUpInside];
        mineButton.titleLabel.font = [UIFont systemFontOfSize:LEFTBARTITLEFOND];
        [self addSubview:mineButton];
        if (i == 0) {
            mineButton.selected = YES;
        }
        //添加提醒红点
        UIView *redView = [[UIView alloc]init];
        redView.backgroundColor = [UIColor redColor];
        redView.frame = CGRectMake(mineButton.frame.size.width+mineButton.frame.origin.x-20, mineButton.frame.origin.y+mineButton.frame.size.height/2-4, 8, 8);
        redView.tag = 50+i;
        redView.hidden = YES;
        redView.layer.cornerRadius = 4;
        redView.layer.masksToBounds = YES;
        [self addSubview:redView];
    }
    
    
}
- (UIButton *)openButton{
    if (!_openButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(180, 7, 26, 30);
        [button setImage:[UIImage imageNamed:@"open"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"close"] forState:UIControlStateSelected];
        button.selected = NO;
        [button addTarget:self action:@selector(openClick:) forControlEvents:UIControlEventTouchUpInside];
        _openButton = button;
    }
    return _openButton;
}
- (NSMutableArray *)buttonArray{
    if (!_buttonArray) {
        NSMutableArray *array = [[NSMutableArray alloc]init];
        _buttonArray = array;
    }
    return _buttonArray;
}
- (NSMutableArray *)imageArray{
    if (!_imageArray) {
        NSMutableArray *array = [[NSMutableArray alloc]init];
        
        _imageArray = array;
    }
    return _imageArray;
}
- (void)reloadLayout:(BOOL)status{
    if (status == YES) {
        _openButton.frame = CGRectMake(73-28, 7, 26, 30);
        for (int i = 0; i<self.buttonArray.count; i++) {
            UIButton *mineButton = (UIButton *)[self viewWithTag:10+i];
            mineButton.frame = CGRectMake(15,44*fh+(H+10)*i,43,40);
            NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"PingFang-SC-Regular" size: 15],NSForegroundColorAttributeName: [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0]}];
            [mineButton setAttributedTitle:string forState:UIControlStateNormal];
        }
    }else{
        _openButton.frame = CGRectMake(180, 7, 26, 30);
        for (int i = 0; i<self.buttonArray.count; i++) {
            UIButton *mineButton = (UIButton *)[self viewWithTag:10+i];
            mineButton.frame = CGRectMake(15,44*fh+(H+10)*i, W, H);
            NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:_buttonArray[i] attributes:@{NSFontAttributeName: [UIFont fontWithName:@"PingFang-SC-Regular" size: 15],NSForegroundColorAttributeName: [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0]}];
            [mineButton setAttributedTitle:string forState:UIControlStateNormal];
        }
    }
    
}
- (void)mineClick:(UIButton *)click{
    for (int i = 0; i<_buttonArray.count; i++) {
        UIButton *b = (UIButton *)[self viewWithTag:10+i];
        b.selected = NO;
    }
    click.selected = YES;
    [self.delegate mineClick:(int)click.tag-10];
}
- (void)selectedButton:(int)index{
    for (int i = 0; i<_buttonArray.count; i++) {
        UIButton *bt = (UIButton *)[self viewWithTag:i+10];
        if (i == index) {
            bt.selected = YES;
        }else{
            bt.selected = NO;
        }
    }
    
}
- (void)openClick:(UIButton *)sender{
    sender.selected = !sender.selected;
    [self teacherStateClick:sender.selected];
}
#pragma mark - 改变左侧菜单按钮
- (void)teacherStateClick:(BOOL)butStatus{
    if (butStatus == YES) {
        self.frame = CGRectMake(0, 20, 70, APP_HEIGHT-20);
        [self reloadLayout:butStatus];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"ChangeCellSpaNotice" object:nil userInfo:@{@"Type":@"1"}];
    }else{
        self.frame = CGRectMake(0, 20, 220, APP_HEIGHT-20);
        [self reloadLayout:butStatus];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"ChangeCellSpaNotice" object:nil userInfo:@{@"Type":@"0"}];
    }
}
@end
